#!/bin/bash 

CONTROL_FILE=DEBIAN/control
LIST_UKUICHROOT=~/packages_with_amd64_suffix.txt

check_depends()
{

 # 使用grep提取Depends信息  
local dependencies=$(grep "^Depends:" "$CONTROL_FILE" | sed 's/Depends://')

 # 使用逗号分隔提取的字符串，并遍历输出
 IFS=',' read -r -a depends_array <<< "$dependencies"
 for item in "${depends_array[@]}"; do
     cleaned_string="${item%% \(*>*}"  #清除包名后面的版本号 
     grep -q $cleaned_string: $LIST_UKUICHROOT
     if [ ! $? -eq 0 ]; then
        echo "The string '$cleaned_string' was not found."
        dependsneeddl_array+="$cleaned_string"
     fi
 done

}


download_depends()
{

 mkdir depend_dir
 cd depend_dir
 for depend in "${dependsneeddl_array}" ; do
     mkdir $depend
     cd $depend
     apt download $depend
     dpkg-deb -R *.deb tmp
     cd ..
 done

}



# 检查是否提供了参数  
if [ "$#" -ne 1 ]; then  
    echo "请提供一个.deb包文件名作为参数"  
    exit 1  
fi  
  
# 检查文件是否存在  
if [ ! -f "$1" ]; then  
    echo "文件 $1 不存在"  
    exit 1  
fi  
  
# 检查文件扩展名是否为.deb  
if [[ "$1" != *.deb ]]; then  
    echo "提供的文件不是一个.deb包"  
    exit 1  
fi 

if [ -d BUILD ]; then
    echo "BUILD路径已存在，移除"
    rm -r BUILD
fi

mkdir BUILD
# 使用dpkg-deb列出.deb包的内容  
dpkg-deb -R "$1" BUILD
cd BUILD

#----------------------分析contrl文件中的depend信息-------------------
# 使用grep提取Depends信息  
dependencies=$(grep "^Depends:" "$CONTROL_FILE" | sed 's/Depends://')

# 使用逗号分隔提取的字符串，并遍历输出
IFS=',' read -r -a depends_array <<< "$dependencies"
for item in "${depends_array[@]}"; do
    cleaned_string="${item%% \(*>*}"  #清除包名后面的版本号 
    grep -q $cleaned_string: $LIST_UKUICHROOT
    if [ ! $? -eq 0 ]; then
       echo "The depend '$cleaned_string' was not found."
       dependsneeddl_array+="$cleaned_string"
    fi
done

mkdir depend_dir
cd depend_dir
for depend in "${dependsneeddl_array}" ; do
    mkdir $depend
    cd $depend
    apt download $depend
    dpkg-deb -R *.deb tmp
    cd ..
done



# 输出Depends信息  
#echo "Depends: $DEPENDS"

